package com.sxj.corejava.code07_面向对象.bms;

import java.util.Scanner;

/**
 * @author 石小俊
 * @date 2024年03月26日 19:02
 */
public class Start {

    private Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        new Start().showMenu();
    }

    public void showMenu() {
        System.out.println("********************欢迎来到紫金银行********************");
        System.out.print("请选择服务(1.开户 2.登录 3.退出):");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                System.out.println("提示:您已退出,欢迎下次光临!");
                break;
            default:
                System.out.println("错误:您输入的选项有误,请重新输入!");
                break;
        }
    }


    public void register() {
        System.out.println("*********************正在开户*********************");
        System.out.print("请输入开户的账户类型(1.储蓄账户 2.信用账户 3.贷款的储蓄账户 4.贷款的信用账户 5.返回菜单):");
        int choice = sc.nextInt();
        Account account = null;
        // 根据所选择的账户类型不同,创建不同的子类对象
        switch (choice) {
            case 1:
                account = new SavingAccount();
                break;
            case 2:
                account = new CreditAccount();
                break;
            case 3:
                account = new LoanSavingAccount();
                break;
            case 4:
                account = new LoanCreditAccount();
                break;
            case 5:
                showMenu();
                break;
            default:
                System.out.println("错误:您输入的选项有误,请重新输入!");
                register();
                break;
        }

        // 录入客户信息
        System.out.print("请输入户主姓名:");
        account.setName(sc.next());
        int index = 0;
        while (true) {
            if (index == 3) {
                System.out.println("错误:您错误的次数过多,已退出!");
                return;
            }
            index++;
            System.out.print("请输入密码:");
            String password = sc.next();
            System.out.print("请再次输入密码:");
            String rePassword = sc.next();
            if (password.equals(rePassword)) {
                // 如果两次输入的密码一致,则退出循环,设置密码的值
                account.setPassword(password);
                break;
            } else {
                System.out.println("错误:两次输入的密码不一致,请重新输入!");
            }
        }
        System.out.print("请输入身份证号码:");
        account.setPersonId(sc.next());
        System.out.print("请输入手机号码:");
        account.setPhone(sc.next());
        // 如果是信用账户
        if (account instanceof CreditAccount) {
            // 将当前账户向下转型为信用账户
            CreditAccount creditAccount = (CreditAccount) account;
            // 还需要指定信用额度
            System.out.print("请输入信用额度:");
            creditAccount.setLimit(sc.nextDouble());

        }
        // 指定完成之后调用银行类提供的开户方法进行开户
        boolean flag = Bank.register(account);
        if (flag) {
            // 如果返回值为true,则说明开户成功
            System.out.println("提示:恭喜您:开户成功,您的账户卡号为:" + account.getId());
        } else {
            System.out.println("错误:对不起,开户失败!");
        }
        // 开户完成之后回到主菜单选项
        showMenu();
    }

    public void login() {
        System.out.println("*********************正在登录*********************");
        System.out.print("请输入卡号:");
        int id = sc.nextInt();
        System.out.print("请输入密码:");
        String password = sc.next();
        // 调用银行类提供的登录方法进行登录
        Account account = Bank.login(id, password);
        // 如果返回的账户对象为null,则说明登录失败,返回错误消息,并返回入口菜单
        if (account == null) {
            System.out.println("错误:卡号或密码错误!");
            showMenu();
        } else {
            // 如果登录成功,进入银行主菜单
            System.out.println("恭喜您,登录成功");
            new BankMenu(account).showMenu();
        }

    }
}
